package com.aidex.biz.controller;

import com.aidex.biz.domain.WorkerList;
import com.aidex.biz.service.WorkerListService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 需求工单Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/biz/bizWorkerList")
public class WorkerListController extends BaseController
{
    @Autowired
    private WorkerListService workerListService;

    /**
     * 查询需求工单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:list')")
    @GetMapping("/list")
    public R<PageInfo> list(WorkerList workerList, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                workerList.setCompanyId(null);
        }else{
                     workerList.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        workerList.setPage(new PageDomain(request, response));
        return R.data(workerListService.findPage(workerList));
    }

    /**
     * 获取需求工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:query')")
    @GetMapping(value = "/{id}")
    public R<WorkerList> detail(@PathVariable("id") String id)
    {
        return R.data(workerListService.get(id));
    }

    /**
     * 新增需求工单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:add')")
    @Log(title = "需求工单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  WorkerList workerList)
    {
        return R.status(workerListService.save(workerList));
    }

    /**
     * 修改需求工单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:edit')")
    @Log(title = "需求工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated WorkerList workerList)
    {
        return R.status(workerListService.save(workerList));
    }


    /**
     * 删除需求工单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:remove')")
    @Log(title = "需求工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(workerListService.deleteWorkerListByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(workerListService.findMaxSort(new WorkerList()));
    }

    /**
     * 导出需求工单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWorkerList:export')")
    @Log(title = "需求工单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(WorkerList workerList)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    workerList.setCompanyId(null);
            }else{
                         workerList.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<WorkerList> list = workerListService.findList(workerList);
        ExcelUtil<WorkerList> util = new ExcelUtil<WorkerList>(WorkerList.class);
        return util.exportExcel(list, "需求工单数据");
    }

}
